package database;

import java.io.Serializable;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * This class represent Supermarket for Hibernate ORM Library
 * @author Moshe Ronen, Lior Bunzl
 *
 */
@Entity
@Table(name = "PRODUCT_has_SUPERMARKET", catalog = "telhai")
@AssociationOverrides({
	@AssociationOverride(name = "pk.product", 
		joinColumns = @JoinColumn(name = "PRODUCT_ID")),
	@AssociationOverride(name = "pk.supermarket", 
		joinColumns = @JoinColumn(name = "SUPERMARKET_ID")) })
public class ProductSupermarket implements Serializable
{
	private static final long serialVersionUID = 1L;
	private ProductSupermarketId pk = new ProductSupermarketId();
	private Double price;
	
	public ProductSupermarket()
	{}

	@EmbeddedId
	public ProductSupermarketId getPk()
	{
		return pk;
	}

	public void setPk(ProductSupermarketId pk)
	{
		this.pk = pk;
	}

	@Transient
	public Product getProduct()
	{
		return getPk().getProduct();
	}
 
	public void setProduct(Product product)
	{
		getPk().setProduct(product);
	}
	
	@Transient
	public Supermarket getSupermarket()
	{
		return getPk().getSupermarket();
	}
 
	public void setSupermarket(Supermarket supermarket)
	{
		getPk().setSupermarket(supermarket);
	}
	
	@Column(name = "PRICE", unique = false, nullable = false)
	public Double getPrice()
	{
		return this.price;
	}

	public void setPrice(Double price)
	{
		this.price = price;
	}
	
	public boolean equals(Object o)
	{
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
 
		ProductSupermarket that = (ProductSupermarket) o;
 
		if (getPk() != null ? !getPk().equals(that.getPk())
				: that.getPk() != null)
			return false;
 
		return true;
	}
 
	public int hashCode()
	{
		return (getPk() != null ? getPk().hashCode() : 0);
	}
	
	@Override
	public String toString()
	{
	    return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
	    .append("Product", pk.getProduct().toString())
	    .append("Supermarket", pk.getSupermarket().toString())
	    .append("Price", price)
	    .toString();
	}
}
